package com.hm.service.request.test;

import com.hm.service.http.HttpUtils;
import com.hm.service.utils.EncryptionUtils;
import com.hm.service.utils.JsonUtils;
import okhttp3.*;

import java.io.IOException;


/**
 * 获取session接口
 */
public class Request06 {

    private static final String url = "http://172.26.2.18:9001/SZCG/MAIN/000_000_006";
    //秘钥
    private static final String secretKey = "kokL8G4RmNfV69cdcJ9dfnrlFGsLy6mH";

    public static void main(String[] args) {
        //String reqParams = JSON.toJSONString(paramsBean);
        //构建请求体
        String encrypt = EncryptionUtils.encrypt(secretKey, "");
        RequestBody body = RequestBody.create(encrypt, MediaType.get("application/json; charset=UTF-8"));
        System.out.println("请求参数-->:" + encrypt);
        Request request = new Request.Builder()//
                .addHeader("sessionId", "QoqXPxZmkpofQy81u4Le3JudATGR4X3chMzuF56q9LVdjL0DWfLeAl8uPsg2K4aEs6LwQjkdWpmJlm9/jYM9WQ4JTFdhKt7UA5mZhgbn+Vk=").url(url)//请求连接
                .post(body)//请求体
                .build();//构建
        //同步请求
        OkHttpClient okHttpClient = HttpUtils.getOkHttpClient();
        try (Response response = okHttpClient.newCall(request).execute()) {
            ResponseBody resBody = response.body();
            int code = response.code();
            if (code == 200 && resBody != null) {
                String jsonString = resBody.string();
                boolean json = JsonUtils.isJson(jsonString);
                System.out.println("ResponseBody-->:" + json);
                System.out.println(json ? jsonString : EncryptionUtils.decrypt(secretKey, jsonString));
            } else {
                System.out.println("ResponseBody为空:");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
